package com.wisdytech.linkdcs.equipment.action;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.BasePageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.ExcelUtil;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.equipment.model.Status;
import com.wisdytech.linkdcs.equipment.service.IEquipmentRegisteredService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentStatusService;

/**
 * 
 * <p>ClassName: EquipStatusAction</p>
 * <p>Description: 设备状态数据</p>
 * <p>Author: dailibo</p>
 * <p>Date: 2017-9-30</p>
 */
@Controller
@RequestMapping(value = "/equipment/status")
public class EquipStatusAction {
    
    @Resource
    private IEquipmentStatusService equipmentStatusService;
    
    /**
     * 注入设备注册equipmentRegisteredService
     */
    @Autowired
    @Qualifier("equipmentRegisterService")
    private IEquipmentRegisteredService equipmentRegisteredService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
     * 
     * <p>Description: 设备状态数据首页</p>
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "equipment/equipStatus/index";
    }
    
    /**
     * 
     * <p>Description: 设备状态查询</p>
     * @param id 设备id
     * @return
     */
    @RequestMapping("/data/index")
    public String dataIndex(Model model,String id, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if(StringUtils.isNotBlank(id)){
            Equipment  equipment =
                    equipmentRegisteredService.getEquipmentRegisterById(id);
            if(null != equipment){
                model.addAttribute("code", equipment.getEquipCode());
            }else{
                model.addAttribute("code", "");
            }
        }else{
            model.addAttribute("code", "");
        }
        
        
        return "equipment/equipStatus/detail";
    }
    
    
    /**
     * 
     * <p>Description: 分页获取设备状态数据</p>
     * @param opId 工序id
     * @param equipId 设备id
     * @param equipName 设备名称
     * @return
     */
    @RequestMapping("/listEquipStatus/get.mvc")
    @ResponseBody
    public StandardResult listStatusByEquipment(String opId,String eqId,String equipName,Filter filter){
        
        BasePageInfo  pageInfo =
                equipmentStatusService.listEquipRealStatus(opId,eqId, equipName, filter);
        
        return StandardResult.getSuccessResult(pageInfo);
    }
    
    /**
     * 
     * <p>Description: 分页获取设备状态数据详情</p>
     * @param opId 工序id
     * @param equipCode 设备id
     * @param equipName 设备名称
     * @return
     */
    @RequestMapping("/listEquipStatusDetail/get.mvc")
    @ResponseBody
    public StandardTableResult<Status> listStatusDetail(String opId,String eqCode,String eqName,Filter filter){
        
        PageInfo<Status>  pageInfo =
                equipmentStatusService.listEquipmentStatusByFilter(opId, eqCode, eqName, filter);
        
        return new StandardTableResult<Status>(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * <p>Description: 导出设备状态信息</p>
     * @param request 请求对象
     * @param response 返回对象
     * @throws Exception 异常
     */
    @RequestMapping(value="/exportStatus/export.mvc")
    public void exportStatus(HttpServletRequest request, HttpServletResponse response, Filter filter) throws Exception {
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String[] columnNames = null;
        String[] fileNames = null;
        String fileName = "设备状态信息";
        String opId = request.getParameter("opId");
        String eqCode = request.getParameter("eqCode");
        String eqName = request.getParameter("eqName");
        if ("null".equals(opId)) {
            opId = "";
        }
        filter.setSize(65535);
        PageInfo<Status> status;
        status =  equipmentStatusService.listEquipmentStatusByFilter(opId, eqCode, eqName, filter);
        List<Status> statuList=status.getList();
        //0--未连接   1--正常  -1---停机
        for (Status statu : statuList) {
            if(statu.getEquipStatus() == 0) {
                statu.setStatus("正常");
            }
            if(statu.getEquipStatus() == 1) {
                statu.setStatus("异常");
            }

        }
        String finalFileName = null;  
        final String userAgent = request.getHeader("USER-AGENT"); 
        if(StringUtils.contains(userAgent, "MSIE")){
            //IE浏览器  
            finalFileName = URLEncoder.encode(fileName,"UTF8");  
        }else {
            finalFileName = fileName;
        }
        columnNames = new String[] { "设备编号", "设备名称", "所属工序", "设备状态说明", "开始时间", "截止时间"};
        fileNames = new String[] { "equipCode", "equipName", "operationName","status","startTime","endTime"};
        ExcelUtil.exportExcel(response, statuList, Status.class, columnNames, fileNames,
                finalFileName + dateFormat.format(new Date()));
    }
    
}
